package cn.ctodb.cms.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.cms.service.CmsTxtService;
import cn.ctodb.cms.web.rest.util.HeaderUtil;
import cn.ctodb.cms.service.dto.CmsTxtDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CmsTxt.
 */
@RestController
@RequestMapping("/api")
public class CmsTxtResource {

    private final Logger log = LoggerFactory.getLogger(CmsTxtResource.class);
        
    @Inject
    private CmsTxtService cmsTxtService;

    /**
     * POST  /cms-txts : Create a new cmsTxt.
     *
     * @param cmsTxtDTO the cmsTxtDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cmsTxtDTO, or with status 400 (Bad Request) if the cmsTxt has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cms-txts")
    @Timed
    public ResponseEntity<CmsTxtDTO> createCmsTxt(@RequestBody CmsTxtDTO cmsTxtDTO) throws URISyntaxException {
        log.debug("REST request to save CmsTxt : {}", cmsTxtDTO);
        if (cmsTxtDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("cmsTxt", "idexists", "A new cmsTxt cannot already have an ID")).body(null);
        }
        CmsTxtDTO result = cmsTxtService.save(cmsTxtDTO);
        return ResponseEntity.created(new URI("/api/cms-txts/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("cmsTxt", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cms-txts : Updates an existing cmsTxt.
     *
     * @param cmsTxtDTO the cmsTxtDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cmsTxtDTO,
     * or with status 400 (Bad Request) if the cmsTxtDTO is not valid,
     * or with status 500 (Internal Server Error) if the cmsTxtDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cms-txts")
    @Timed
    public ResponseEntity<CmsTxtDTO> updateCmsTxt(@RequestBody CmsTxtDTO cmsTxtDTO) throws URISyntaxException {
        log.debug("REST request to update CmsTxt : {}", cmsTxtDTO);
        if (cmsTxtDTO.getId() == null) {
            return createCmsTxt(cmsTxtDTO);
        }
        CmsTxtDTO result = cmsTxtService.save(cmsTxtDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("cmsTxt", cmsTxtDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cms-txts : get all the cmsTxts.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cmsTxts in body
     */
    @GetMapping("/cms-txts")
    @Timed
    public List<CmsTxtDTO> getAllCmsTxts() {
        log.debug("REST request to get all CmsTxts");
        return cmsTxtService.findAll();
    }

    /**
     * GET  /cms-txts/:id : get the "id" cmsTxt.
     *
     * @param id the id of the cmsTxtDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cmsTxtDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cms-txts/{id}")
    @Timed
    public ResponseEntity<CmsTxtDTO> getCmsTxt(@PathVariable Long id) {
        log.debug("REST request to get CmsTxt : {}", id);
        CmsTxtDTO cmsTxtDTO = cmsTxtService.findOne(id);
        return Optional.ofNullable(cmsTxtDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /cms-txts/:id : delete the "id" cmsTxt.
     *
     * @param id the id of the cmsTxtDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cms-txts/{id}")
    @Timed
    public ResponseEntity<Void> deleteCmsTxt(@PathVariable Long id) {
        log.debug("REST request to delete CmsTxt : {}", id);
        cmsTxtService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("cmsTxt", id.toString())).build();
    }

    /**
     * SEARCH  /_search/cms-txts?query=:query : search for the cmsTxt corresponding
     * to the query.
     *
     * @param query the query of the cmsTxt search 
     * @return the result of the search
     */
    @GetMapping("/_search/cms-txts")
    @Timed
    public List<CmsTxtDTO> searchCmsTxts(@RequestParam String query) {
        log.debug("REST request to search CmsTxts for query {}", query);
        return cmsTxtService.search(query);
    }


}
